package com.youlai.boot.gym.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.gym.mapper.EquipmentsMapper;
import com.youlai.boot.gym.service.EquipmentsService;
import com.youlai.boot.gym.model.entity.Equipments;
import com.youlai.boot.gym.model.form.EquipmentsForm;
import com.youlai.boot.gym.model.query.EquipmentsQuery;
import com.youlai.boot.gym.model.vo.EquipmentsVO;
import com.youlai.boot.gym.converter.EquipmentsConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 器材服务实现类
 *
 * @author hxc
 * @since 2025-06-01 10:04
 */
@Service
@RequiredArgsConstructor
public class EquipmentsServiceImpl extends ServiceImpl<EquipmentsMapper, Equipments> implements EquipmentsService {

    private final EquipmentsConverter equipmentsConverter;

    /**
    * 获取器材分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<EquipmentsVO>} 器材分页列表
    */
    @Override
    public IPage<EquipmentsVO> getEquipmentsPage(EquipmentsQuery queryParams) {
        Page<EquipmentsVO> pageVO = this.baseMapper.getEquipmentsPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取器材表单数据
     *
     * @param id 器材ID
     * @return 器材表单数据
     */
    @Override
    public EquipmentsForm getEquipmentsFormData(Long id) {
        Equipments entity = this.getById(id);
        return equipmentsConverter.toForm(entity);
    }
    
    /**
     * 新增器材
     *
     * @param formData 器材表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveEquipments(EquipmentsForm formData) {
        Equipments entity = equipmentsConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新器材
     *
     * @param id   器材ID
     * @param formData 器材表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateEquipments(Long id,EquipmentsForm formData) {
        Equipments entity = equipmentsConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除器材
     *
     * @param ids 器材ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteEquipmentss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的器材数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
